package cc.vv.baselibrary.adapter.holder

import android.support.annotation.IdRes
import android.view.View
import android.widget.ImageView
import cc.vv.baselibrary.R
import cc.vv.baselibrary.view.LKAvatarView
import cc.vv.baselibrary.view.OvalImageView
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import com.chad.library.adapter.base.BaseViewHolder

/**
 *  添加扩展的 ViewHolder
 * @author Zml
 * @date 2018/7/20.
 */
class ExtendViewHolder(view: View) : BaseViewHolder(view) {
    /**
     * 设置头像图片
     */
    fun setImage(@IdRes viewId: Int, imgUrl: String?): ExtendViewHolder {
        getView<LKAvatarView>(viewId)?.initAvatar("", imgUrl, R.mipmap.default_user_avatar)
//        val view = getView<LKAvatarView>(viewId)
//        LKImage.load().load(imgUrl)
//                .thumbnail(0.5f) // 加载略缩图
//                .error(R.mipmap.default_user_avatar)// 加载异常时默认图片
//                .into(view.imageView)
        return this
    }

    /**
     * 设置图片
     */
    fun setImage(@IdRes viewId: Int, imgUrl: String?, defResId: Int): ExtendViewHolder {
        val view = getView<ImageView>(viewId)
        if (null != view) {
            LKImage.load().load(imgUrl)
                    .thumbnail(0.3f) // 加载略缩图
                    .error(defResId)// 加载异常时默认图片
                    .into(view)
        }
        return this
    }

    /**
     * 加载图片
     */
    fun loadImage(@IdRes viewId: Int, imgUrl: String?): ExtendViewHolder {
        val view = getView<ImageView>(viewId)
        if (null != view) {
            LKImage.load().load(imgUrl).scale(ScaleMode.CENTER_CROP)
                    .thumbnail(0.3f) // 加载略缩图
                    .error(R.mipmap.icon_def_image)// 加载异常时默认图片
                    .into(view)
        }
        return this
    }

    /**
     * 加载圆角图片
     */
    fun loadImageRadius(@IdRes viewId: Int, imgUrl: String?): ExtendViewHolder {
        val view = getView<OvalImageView>(viewId)
        if (null != view) {
            LKImage.load().load(imgUrl).scale(ScaleMode.CENTER_CROP)
                    .thumbnail(0.3f) // 加载略缩图
                    .error(R.mipmap.icon_def_image)// 加载异常时默认图片
                    .into(view)
        }
        return this
    }


    /**
     * 设置控件是否启用
     */
    fun setEnabled(@IdRes viewId: Int, isEnabled: Boolean = true): ExtendViewHolder {
        getView<View>(viewId).isEnabled = isEnabled
        return this
    }
}